<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="dataForm.title" placeholder="标题"></el-input>
      </el-form-item>
<!--      <el-form-item label="图片url" prop="url">-->
<!--        <el-input v-model="dataForm.url" placeholder="图片url"></el-input>-->
<!--      </el-form-item>-->
        <el-form-item label="上传图片" prop="url">
          <el-upload
            class="avatar-uploader"
            :headers="header"
            :action="$http.adornUrl('/generator/pworks/upload')"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      <el-form-item label="地点" prop="place">
        <el-input v-model="dataForm.place" placeholder="地点"></el-input>
      </el-form-item>
      <el-form-item label="满意度" prop="score">
        <el-input v-model="dataForm.score" placeholder="满意度"></el-input>
<!--        <el-rate-->
<!--          v-model="dataForm.score"-->
<!--          :colors="colors">-->
<!--        </el-rate>-->
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <el-input v-model="dataForm.content" placeholder="内容"></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-input v-model="dataForm.type" placeholder="类型"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import Vue from 'vue'

export default {
  data () {
    return {
      header: {
        token: Vue.cookie.get('token')
      },
      colors: ['#99A9BF', '#F7BA2A', '#FF9900'],
      imageUrl: '',
      visible: false,
      dataForm: {
        id: 0,
        title: '',
        url: '',
        place: '',
        score: '',
        content: '',
        type: ''
      },
      dataRule: {
        title: [
          {required: true, message: '标题不能为空', trigger: 'blur'}
        ],
        url: [
          {required: true, message: '图片url不能为空', trigger: 'blur'}
        ],
        place: [
          {required: true, message: '地点不能为空', trigger: 'blur'}
        ],
        score: [
          {required: true, message: '满意度不能为空', trigger: 'blur'}
        ],
        content: [
          {required: true, message: '内容不能为空', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '类型不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    handleAvatarSuccess (res, file) {
      this.dataForm.url = file.response.url
      this.imageUrl = this.$http.adornUrl(file.response.url)
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    init (id) {
      this.imageUrl = ''
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/generator/pworks/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              console.log(data)
              this.dataForm.title = data.pWorks.title
              this.imageUrl = data.pWorks.url
              this.dataForm.url = this.imageUrl
              this.dataForm.place = data.pWorks.place
              this.dataForm.score = data.pWorks.score
              this.dataForm.content = data.pWorks.content
              this.dataForm.type = data.pWorks.type
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/generator/pworks/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'title': this.dataForm.title,
              'url': this.imageUrl,
              'place': this.dataForm.place,
              'score': this.dataForm.score,
              'content': this.dataForm.content,
              'type': this.dataForm.type
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
